package com.nononsenseapps.feeder.ui.reader.code

import android.os.Bundle
import android.view.*
import androidx.lifecycle.lifecycleScope
import com.nononsenseapps.feeder.ui.R
import com.nononsenseapps.feeder.ui.base.CoroutineScopedFragment
import com.nononsenseapps.feeder.util.TabletUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import thereisnospon.codeview.CodeView
import thereisnospon.codeview.CodeViewTheme


class CodeViewFragment : CoroutineScopedFragment() {

    lateinit var codeView: CodeView
     var htmlCode:String?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        val theLayout = if (TabletUtils.isTablet(activity)) {
            R.layout.fragment_codeview
        } else {
            R.layout.fragment_codeview
        }
        val rootView = inflater.inflate(theLayout, container, false)
        codeView = rootView.findViewById(R.id.codeview)
        codeView.setTheme(CodeViewTheme.ANDROIDSTUDIO)?.fillColor()
        //这里的CODE 为需要显示的代码，类型为String，使用的时候自己替换下。
        return rootView
    }


    override fun onStart() {
        super.onStart()
        if(htmlCode!=null) return
        lifecycleScope.launch(Dispatchers.IO) {
            feedItemDao.loadFeedItem(globalViewModel.currentItemId())?.description?.let{
                htmlCode=it
                withContext(Dispatchers.Main) {
                    codeView.showCode(it)
                }
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.code_view, menu)
    }

    override fun onOptionsItemSelected(menuItem: MenuItem): Boolean =
            when (menuItem.itemId) {
                R.id.action_format_code -> {
                    lifecycleScope.launch(Dispatchers.IO) {
                        htmlCode?.also {
                            val doc: Document = Jsoup.parse(it)
                            htmlCode = doc.toString()
                            withContext(Dispatchers.Main) {
                                codeView.showCode(htmlCode)
                            }
                        }
                    }
                    true
                }
                R.id.action_code_truncate -> {
                    /*launch(Dispatchers.IO) {
                        htmlCode=truncateHTML(htmlCode,1000)
                        withContext(Dispatchers.Main) {
                            codeView?.showCode(htmlCode)
                        }
                    }*/
                    true
                }
                else -> super.onOptionsItemSelected(menuItem)
            }

}